package com.im.common.netty.my.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 私有协议编码器
 */
public class PrivateProtocolEncoder extends MessageToByteEncoder<PrivateProtocol> {

    @Override
    protected void encode(ChannelHandlerContext ctx, PrivateProtocol msg, ByteBuf out) throws Exception {
        // 写入协议头
        out.writeInt(msg.getBody().length);
        // 写入协议体
        out.writeBytes(msg.getBody());
    }

}